﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Admin_quan_ly_phu_hop : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetAllAppropriate();
            GetAllRes();
        }
    }
    protected void GetAllAppropriate()
    {
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllAppropriate";
            DataTable db = Dal.GetDataTable();
            grvAppropriate.DataSource = db;
            grvAppropriate.DataBind();
            ddlAppropriate.DataSource = db;
            ddlAppropriate.DataTextField = db.Columns["MoTa"].ToString();
            ddlAppropriate.DataValueField = db.Columns["PhuHopID"].ToString();
            ddlAppropriate.DataBind();
        }
        catch { }
    }
    protected void grvAppropriate_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvAppropriate.EditIndex = -1;
        GetAllAppropriate();
    }
    protected void grvAppropriate_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int i = Convert.ToInt32(grvAppropriate.DataKeys[e.RowIndex].Value);
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_DeleteAppropriate";
            Dal.ParameterCollection = new string[1] { "@PhuHopID"};
            Dal.ValueCollection = new string[1] { Convert.ToString(i)};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAllAppropriate();
        }
        catch
        { }
    }
    protected void grvAppropriate_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvAppropriate.EditIndex = e.NewEditIndex;
        GetAllAppropriate();
        ((TextBox)grvAppropriate.Rows[e.NewEditIndex].Cells[0].Controls[0]).Enabled = true;
    }
    protected void grvAppropriate_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string i = Convert.ToString(grvAppropriate.DataKeys[e.RowIndex].Value);
        string mota = ((TextBox)(grvAppropriate.Rows[e.RowIndex].Cells[0].Controls[0])).Text;
        FileUpload fuAvatar = (FileUpload)(grvAppropriate.Rows[e.RowIndex].FindControl("fuAvatar"));
        if (fuAvatar.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(fuAvatar.FileName);
                fuAvatar.SaveAs(Server.MapPath("~/Images/PhuHopIcon/" + filename));
                SQLHelper Dal = new SQLHelper();
                Dal.Open();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_UpdateAppropriateWithImage";
                Dal.ParameterCollection = new string[3] { "@MoTa", "@PhuHopID", "@HinhDaiDien" };
                Dal.ValueCollection = new string[3] { mota, i, "Images/PhuHopIcon/" + filename };
                Dal.ExecuteNonQuery();
                Dal.Close();
                Dal.Dispose();
            }
            catch { }
        }
        else
        {
            try
            {
                SQLHelper Dal = new SQLHelper();
                Dal.Open();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_UpdateAppropriateWithNoneImage";
                Dal.ParameterCollection = new string[2] { "@MoTa", "@PhuHopID"};
                Dal.ValueCollection = new string[2] { mota, i};
                Dal.ExecuteNonQuery();
                Dal.Close();
                Dal.Dispose();
            }
            catch { }
        }
        grvAppropriate.EditIndex = -1;
        GetAllAppropriate();
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        if (fuAvatar.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(fuAvatar.FileName);
                fuAvatar.SaveAs(Server.MapPath("~/Images/PhuHopIcon/"+filename));
                SQLHelper Dal = new SQLHelper();
                Dal.Open();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_AddAppropriate";
                Dal.ParameterCollection = new string[2] { "@MoTa", "@HinhDaiDien" };
                Dal.ValueCollection = new string[2] { txtAppropriateDes.Text, "Images/PhuHopIcon/"+filename };
                Dal.ExecuteNonQuery();
                Dal.Close();
                Dal.Dispose();
                GetAllAppropriate();
                txtAppropriateDes.Text = "";
            }
            catch { }
        }
    }
    protected void GetAllRes()
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetRes";
            DataTable db = Dal.GetDataTable();
            ddlRes.DataSource = db;
            ddlRes.DataTextField = db.Columns["TenNhaHang"].ToString();
            ddlRes.DataValueField = db.Columns["NhaHangID"].ToString();
            ddlRes.DataBind();
        }
        catch { }
    }
    protected void GetAppropriatebyResID()
    {
       
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAppropriateByResID";
            Dal.ParameterCollection = new string[1] { "@NhaHangID"};
            Dal.ValueCollection = new string[1] { ddlRes.SelectedValue};
            DataTable db = Dal.GetDataTable();
            grvResAppropriate.DataSource = db;
            grvResAppropriate.DataBind();
       
    }

    protected void grvResAppropriate_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string i = Convert.ToString(grvResAppropriate.DataKeys[e.RowIndex].Value);
        DropDownList ddl = (DropDownList)(grvResAppropriate.Rows[e.RowIndex].FindControl("ddlAppropriate"));
        //try
        //{
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_UpdateAppropriateRes";
            CheckBox IsDefault = (CheckBox)(grvResAppropriate.Rows[e.RowIndex].Cells[2].Controls[0]);
            Dal.ParameterCollection = new string[3] { "@NhaHang_PhuHopID", "@PhuHopID","@IsDefault" };
            Dal.ValueCollection = new string[3] { i, ddl.SelectedValue, Convert.ToString(IsDefault.Checked) };
            Dal.ExecuteNonQuery();
            grvResAppropriate.EditIndex = -1;
            Dal.Close();
            Dal.Dispose();
            GetAppropriatebyResID();
        //}
        //catch { }
    }
    protected void grvResAppropriate_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvResAppropriate.EditIndex = e.NewEditIndex;
        GetAppropriatebyResID();

    }
    protected void grvResAppropriate_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string i = Convert.ToString(grvResAppropriate.DataKeys[e.RowIndex].Value);
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_DeleteResAppropriate";
            Dal.ParameterCollection = new string[1] { "@NhaHang_PhuHopID"};
            Dal.ValueCollection = new string[1] { i};
            Dal.ExecuteNonQuery();
            GetAppropriatebyResID();
        }
        catch { }
    }
    protected void grvResAppropriate_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //try
        //{
            DataRowView drv = e.Row.DataItem as DataRowView;
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                SQLHelper Dal = new SQLHelper();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_GetAllAppropriate";
                DataTable db = Dal.GetDataTable();
                DropDownList ddl = new DropDownList();
                ddl = (DropDownList)e.Row.FindControl("ddlAppropriate");
                ddl.DataSource = db;
                ddl.DataTextField = db.Columns["MoTa"].ToString();
                ddl.DataValueField = db.Columns["PhuHopID"].ToString();
                ddl.SelectedValue = drv[1].ToString();
                ddl.DataBind();
            }
        //}
        //catch { }
    }
    protected void grvResAppropriate_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvResAppropriate.EditIndex = -1;
        GetAppropriatebyResID();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_AddAppropriateToRes";
            Dal.ParameterCollection = new string[3] { "@PhuHopID", "@NhaHangID","@NgayTao" };
            Dal.ValueCollection = new string[3] { ddlAppropriate.SelectedValue,ddlRes.SelectedValue,DateTime.Now.ToString()};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAppropriatebyResID();

        }
        catch { }
    }
    protected void ddlRes_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetAppropriatebyResID();
    }
}
